package com.example.day16_mp;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class SqlUtils {
    //用到的参数
    private MySql mySql;
    private SQLiteDatabase db;
    private SqlUtils(Context context){
        mySql = new MySql(context);
        db = mySql.getWritableDatabase();
    }
    private static SqlUtils sqlUtils = null;
    public static SqlUtils getInstance(Context context){
        if(sqlUtils==null){
            sqlUtils = new SqlUtils(context);
        }
        return sqlUtils;
    }

    //收藏方法
    public void insert(Music.DataBean dataBean){
        ContentValues values = new ContentValues();
        values.put("picUrl",dataBean.getPicUrl());
        values.put("name",dataBean.getName());
        values.put("duration",dataBean.getDuration());
        values.put("data",dataBean.getData());
        db.insert("music",null,values);
    }
    //查询方法
    public List<Music.DataBean> query(){
        List<Music.DataBean> list = new ArrayList<>();
        Cursor cursor = db.query("music", null, null, null, null, null, null);
        if(cursor!=null){
            while (cursor.moveToNext()){
                @SuppressLint("Range") String url = cursor.getString(cursor.getColumnIndex("picUrl"));
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
                @SuppressLint("Range") String data = cursor.getString(cursor.getColumnIndex("data"));
                @SuppressLint("Range") int duration = cursor.getInt(cursor.getColumnIndex("duration"));
                list.add(new Music.DataBean(name,data,url,duration));
            }
            cursor.close();
        }
        return list;
    }
    //删除方法
    public void shan(String name){
        db.delete("music","name=?",new String[]{name});
    }
}
